#include "SteppingAction.hh"

using namespace std;

SteppingAction::SteppingAction()
{
}

SteppingAction::SteppingAction(HistoManager* HistoManager, Data* Data)
  : histoManager(HistoManager), data(Data)
{
  Tuple = HistoManager->GetMiniBallTuple();
}

SteppingAction::~SteppingAction()
{
}

void SteppingAction::UserSteppingAction(const G4Step* aStep)
{
  if(Tuple) 
    {
      if(data->VerboseLevel > 7)
	{
	  cout<<"UserSteppingAction started:"<<flush;
	  G4String proc;
	  if(fpSteppingManager->GetfPostStepPoint()->GetProcessDefinedStep() != 0)
	    {
	      proc = fpSteppingManager->GetfPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
	    }
	  else
	    {
	      proc = "OutOfWorldTransportation";
	    }
	  G4String vol;
	  if(fpSteppingManager->GetTrack()->GetNextVolume() != 0) 
	    { 
	      vol = fpSteppingManager->GetTrack()->GetVolume()->GetName();
	    }
	  else
	    {
	      vol = "OutOfWorld";
	    }
	  G4String proc_pre;
	  if(fpSteppingManager->GetfPreStepPoint()->GetProcessDefinedStep() != 0)
	    {
	      proc_pre = fpSteppingManager->GetfPreStepPoint()->GetProcessDefinedStep()->GetProcessName();
	    }
	  else
	    {
	      proc_pre = "Beginning";
	    }
	  G4ThreeVector pre = fpSteppingManager->GetfPreStepPoint()->GetPosition();
	  G4ThreeVector post = fpSteppingManager->GetfPostStepPoint()->GetPosition();
	  G4ThreeVector diff = post - pre;
	  cout<<" "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetParticleName()<<" deposits in volume "<<vol<<" with "<<proc_pre<<"/"<<proc<<" "<<aStep->GetTotalEnergyDeposit()<<" MeV (E_kin = "<<fpSteppingManager->GetfPreStepPoint()->GetKineticEnergy()<<" MeV)"<<endl;
	  cout<<"\tstep status = "<<fpSteppingManager->GetfPostStepPoint()->GetStepStatus()<<", PreStepPoint: ("<<pre.x()<<","<<pre.y()<<","<<pre.z()<<") = ("<<pre.theta()<<","<<pre.phi()<<") , PostStepPoint: ("<<post.x()<<","<<post.y()<<","<<post.z()<<") = ("<<pre.theta()<<","<<pre.phi()<<")"<<endl;
	  cout<<"direction change: deltaTheta = "<<diff.theta()-pre.theta()<<", deltaPhi = "<<diff.phi()-pre.phi()<<endl;
	}
      if(fpSteppingManager->GetTrack()->GetCurrentStepNumber() == 1 && data->VerboseLevel > 7)
	{
	  cout<<"Particle "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetParticleName()<<": "
	      <<"PDGCharge = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetPDGCharge()<<", "
	      <<"PDGMass = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetPDGMass()<<", "
	      <<"Charge = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetCharge()<<", "
	      <<"Mass = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetBaryonNumber()<<endl;
	}
      
      if(data->VerboseLevel > 7)
	{
	  cout<<"Particle "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetParticleName()<<": "
	      <<"PDGCharge = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetPDGCharge()<<", "
	      <<"PDGMass = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetPDGMass()<<", "
	      <<"Charge = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetCharge()<<", "
	      <<"Mass = "<<fpSteppingManager->GetTrack()->GetDynamicParticle()->GetDefinition()->GetBaryonNumber()<<endl;
	}
    }
}
